package org.vergil.core.guava.Thread;

import com.google.common.eventbus.EventBus;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * Created by vergil on 4/23/17.
 */
public class SetThread implements Runnable {

    private EventBus eventBus;

    final static Logger log = LoggerFactory.getLogger(SetThread.class);

    private volatile int count = 1000;

    public SetThread(EventBus eventBus){
        this.eventBus = eventBus;
        eventBus.register(this);
    }

    @Override
    public void run() {
        while (true) {
            synchronized (this) {
                if (count < 0){
                    return;
                }
                eventBus.post(count);
                log.info("{} set count is {}", Thread.currentThread().getName(), count);
                count--;
            }
//                try {
//                    Thread.sleep(1000);
//                } catch (InterruptedException e) {
//                    e.printStackTrace();
//                }
        }
    }
}
